package com.dodoke.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dodoke.dto.RetailAccountDTO;
import com.dodoke.entity.RetailAccount;
import com.dodoke.entity.RetailArea;
import com.dodoke.entity.RetailStore;
import com.dodoke.mapper.RetailAccountMapper;
import com.dodoke.mapper.RetailAreaMapper;
import com.dodoke.mapper.RetailStoreMapper;
import com.dodoke.service.IServiceRetailAccount;
import com.dodoke.vo.RetailAccountDetailVO;
import com.dodoke.vo.RetailAccountListVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 人员/商户业务层实现
 */
@Service
public class RetailAccountServiceImpl extends ServiceImpl<RetailAccountMapper, RetailAccount> implements IServiceRetailAccount {

    @Resource
    private RetailAccountMapper retailAccountMapper;
    @Resource
    private RetailStoreMapper retailStoreMapper;
    @Resource
    private RetailAreaMapper retailAreaMapper;

    /**
     * 人员/商户分页列表
     *
     * @param currentPage      当前页码
     * @param pageSize         每页显示条数
     * @param retailAccountDTO 模糊查询条件
     * @return
     */
    @Override
    public Page<RetailAccountListVO> getListByPage(Integer currentPage, Integer pageSize, RetailAccountDTO retailAccountDTO) {
        Page<RetailAccountListVO> page = new Page<>(currentPage, pageSize);
        page = retailAccountMapper.selectByList(page, retailAccountDTO);
        return page;
    }

    /**
     * 新增人员/商户
     *
     * @param retailAccount
     * @return
     */
    @Override
    public int saveRetailAccount(RetailAccount retailAccount) {
        retailAccount.setAccountName(retailAccount.getPhone());
        retailAccount.setIsMain("0");
        retailAccount.setStatus("1");
        retailAccount.setDelFlag("0");
        return retailAccountMapper.insertRetailAccount(retailAccount);
    }

    /**
     * 编辑人员/商户
     *
     * @param retailAccount
     * @return
     */
    @Override
    public int editRetailAccount(RetailAccount retailAccount) {
        return retailAccountMapper.updateRetailAccount(retailAccount);
    }

    /**
     * 根据id获取人员/商户信息
     *
     * @param id
     * @return
     */
    @Override
    public RetailAccountDetailVO getById(Long id) {
        RetailAccount retailAccount = retailAccountMapper.selectById(id);
        RetailAccountDetailVO retailAccountDetailVO = JSON.parseObject(JSON.toJSONString(retailAccount), RetailAccountDetailVO.class);
        return retailAccountDetailVO;
    }

    /**
     * 根据id设置负责人
     *
     * @param id
     */
    @Override
    @Transactional
    public boolean setIsMain(Long id) {
        // 1.根据当前id获取当前门店/网点或者商户信息
        RetailAccount retailAccount = retailAccountMapper.selectById(id);
        // 2.清空所有负责人该人员/商户负责人
        retailAccountMapper.clearMainPersonByFromId(retailAccount.getFromId(), retailAccount.getType());
        // 3.设置当前负责人
        int resCount = retailAccountMapper.setMainPerson(id);
        // 如果是门店/网点,再在主表进行设置
        if ("0".equals(retailAccount.getType())){
            RetailStore retailStore = retailStoreMapper.selectById(retailAccount.getFromId());
            retailStore.setMainPerson(retailAccount.getName() + "/" + retailAccount.getPhone());
            retailStore.setUpdateDate(LocalDateTime.now());
            retailStoreMapper.updateById(retailStore);
        }else if ("1".equals(retailAccount.getType())){
            RetailArea retailArea = retailAreaMapper.selectById(retailAccount.getFromId());
            retailArea.setMainPerson(retailAccount.getName());
            retailArea.setUpdateDate(LocalDateTime.now());
            retailAreaMapper.updateById(retailArea);
        }
        return resCount > 0 ? true : false;
    }
}
